﻿/**
 * @auth Chunning
 * 
 * @purpose ComboBox.
 * 
 * @date 2009-3-18
 * 
 * */
package com.components.form
{
	import com.components.button.ButtonEvents;
	import com.components.button.ButtonGroup;
	import com.components.button.MCButton;
	import com.components.button.ToggleButton;
	import com.components.scrollpane.ScrollPane;
	
	import core.section.Controller;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	
	import gs.TweenLite;
	
	public class ComboBox extends Controller
	{
		//START...
		
		//PROPERTY...
		private var tar:Sprite;
		
		private var buttonGroup:ButtonGroup;
		
		private var btnExpand:ToggleButton;
		
		private var selectedItemContainer:Sprite;
		
		private var itemList:ScrollPane;
		
		private var contentList:Sprite;
				
		private var selectedBp:Bitmap;
		
		//CONSTRUCTOR...
		public function ComboBox(target:DisplayObject)
		{
		
			tar = target as Sprite;
		
			super(target);
			
			init();
		
		}
		
		//METHODS...
		
		//init...
		private function init():void
		{
			
			btnExpand = new ToggleButton(tar.getChildByName("btnExpand") as Sprite);
			
			selectedItemContainer = tar.getChildByName("selectedItemContainer") as Sprite;
			
			selectedBp = new Bitmap;
			
			selectedItemContainer.addChild(selectedBp);
			
			itemList = new ScrollPane(tar.getChildByName("itemList") as Sprite);
									
			contentList = new Sprite;
			
			itemList.content = contentList;
			
			buttonGroup = new ButtonGroup;
			
			itemList.target.visible = false;
			
			itemList.target.alpha = 0;
						
			configEventListeners();
			
		}
		
		//config event listener...
		private function configEventListeners():void
		{
									
			buttonGroup.addEventListener(ButtonEvents.BUTTON_SELECTED,groupSelectedHandler);
			
			btnExpand.addEventListener(ButtonEvents.BUTTON_ON,btnOffHandler);
			
			btnExpand.addEventListener(ButtonEvents.BUTTON_OFF,btnOnHandler);
			
		}
				
		
		//button on handler...
		private function btnOnHandler(evt:ButtonEvents):void
		{
			
			close();
				
		}
		
		//hide list...
		private function hideList():void
		{
			
			itemList.target.visible = false;
			
		}
		
		//button off handler...
		private function btnOffHandler(evt:ButtonEvents):void
		{
			
			open();
			
			
		}
		
		//group selected handler...
		private function groupSelectedHandler(evt:ButtonEvents):void
		{
			
			var button:MCButton  = evt.selectedItem;
			
			var bd:BitmapData = new BitmapData(selectedItemContainer.width,selectedItemContainer.height,true,0x00000000);
			
			bd.draw((button.target as Sprite).getChildByName("content"));
			
			selectedBp.bitmapData = bd;		
			
			close();
			
			dispatchEvent(new Event(Event.CHANGE));
			
		}
		
		//add item...
		public function addItem(item:MCButton):void
		{
			
			buttonGroup.addButton(item);
			
			item.target.x = 0;
			
			item.target.y = contentList.height;
			
			contentList.addChild(item.target);
			
			itemList.update();			
			
		}
		
		//add items...
		public function addItems(...items):void
		{
			
			buttonGroup.addButtons(items);
			
		}
		
				
		//set selected item...
		public function set selectedItem(item:MCButton):void
		{
			
			buttonGroup.selectButton(item);
			
		}
		
		//get selected item...
		public function get selectedItem():MCButton
		{
			
			return buttonGroup.selectedButton;
			
		}
		
		//set selected index...
		public function set selectedIndex(index:uint):void
		{
			
			buttonGroup.selectButtonByIndex(index);
						
		}
		
		//get selected index...
		public function get selectedIndex():uint
		{
			
			return buttonGroup.selectedIndex;
			
		}
		
		
		//remove all items...
		public function removeAllItems():void
		{
			
			selectedBp.bitmapData = new BitmapData(selectedItemContainer.width,selectedItemContainer.height,true,0x00000000);
						
			for(var i:uint = 0; i < buttonGroup.buttonCount;i++)
			{
				
				contentList.removeChild(buttonGroup.getButtonByIndex(i).target);
												
			}
			
			buttonGroup.removeAll();							
			
			if(itemList.hScrollBar) itemList.hScrollBar.position = 0;
			
			if(itemList.vScrollBar) itemList.vScrollBar.position = 0;
															
			itemList.update();
			
		}
		
		//return number of items...
		public function get itemsCount():uint
		{
			
			return buttonGroup.buttonCount;
			
		}
		
		//open...
		public function open():void
		{
			
			btnExpand.state = ToggleButton.ON_STATE;
			
			itemList.target.visible = true;
			
			new TweenLite(itemList.target,.5,{alpha:1});
			
			
		}
		
		//close...
		public function close():void
		{
			
			btnExpand.state = ToggleButton.OFF_STATE;
			
			itemList.target.visible = true;
			
			new TweenLite(itemList.target,.5,{alpha:0,onComplete:hideList});
			
			
		}
		
		
		
		
		//END
	}
}